//
//  LoginTypeView.m
//  us
//
//  Created by karry on 2025/3/29.
//

#import "LoginTypeView.h"

@implementation LoginTypeView

- (void)awakeFromNib{
    [super awakeFromNib];
    
    // 1. 创建文本
    NSString *text = @"继续表示同意《隐私协议》和《用户条款》";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    
    // 2. 设定链接范围
    NSRange privacyRange = [text rangeOfString:@"《隐私协议》"];
    NSRange termsRange = [text rangeOfString:@"《用户条款》"];
    
    // 3. 设置蓝色 & 可点击
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:privacyRange];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:termsRange];
    
    
    // 4. 设置可点击（使用 NSTextAttachment）
    [attributedString addAttribute:NSLinkAttributeName value:@"privacy://" range:privacyRange];
    [attributedString addAttribute:NSLinkAttributeName value:@"terms://" range:termsRange];
    
    // 5. 赋值给 UITextView
    self.privacyTextView.attributedText = attributedString;
    self.privacyTextView.selectable = YES;
    self.privacyTextView.userInteractionEnabled = YES;
    self.privacyTextView.editable = NO; // 不允许编辑
    self.privacyTextView.scrollEnabled = NO; // 不允许滚动
    self.privacyTextView.dataDetectorTypes = UIDataDetectorTypeLink; // 允许识别链接
    self.privacyTextView.delegate = self; // 监听点击事件
    self.privacyTextView.textAlignment = NSTextAlignmentCenter;
    
}
// 处理文本链接点击
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    // 根据URL判断点击的链接
    if ([URL.absoluteString isEqualToString:@"privacy://"]) {
        // 点击了《隐私协议》
        if ([self.delegate respondsToSelector:@selector(loginTypeViewDidTapPrivacy:)]) {
            [self.delegate loginTypeViewDidTapPrivacy:self];
        }
        return NO; // 阻止文本链接的默认行为（比如打开网页）
    } else if ([URL.absoluteString isEqualToString:@"terms://"]) {
        // 点击了《用户条款》
        if ([self.delegate respondsToSelector:@selector(loginTypeViewDidTapTerms:)]) {
            [self.delegate loginTypeViewDidTapTerms:self];
        }
        return NO;
    }
    
    return YES; // 允许其他链接正常处理
}


- (IBAction)WechatAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(loginWithWechat)]) {
        [self.delegate loginWithWechat];
    }
}


- (IBAction)appleAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(loginWithApple)]) {
        [self.delegate loginWithApple];
    }
}

- (IBAction)qqAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(loginWithQQ)]) {
        [self.delegate loginWithQQ];
    }
}

@end
